//
//  MineViewController.m
//  ScalperMerchant
//
//  Created by 王云 on 2017/9/30.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "MineViewController.h"
#import "MineHeaderView.h"
#import "SettingController.h"
#import "BillViewController.h"
#import "MineClerkController.h"
#import "MineShopController.h"
#import "VolumeController.h"
#import "SettlementController.h"

@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak)MineHeaderView *headerV;
@property (nonatomic,weak)UITableView *tableView;

@property (nonatomic,strong)NSArray *data;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setUI];
    [self initData];
}
- (void)setUI{
    
//    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"arrowleft" highIcon:@"" target:self action:@selector(pop)];

    
    __weak __typeof(&*self)weakSelf = self;
    [self.view addSubview:self.tableView];
    
    MineHeaderView *headerV = [[MineHeaderView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 200) option:^(MineBtnClick btnType) {
        
    }];     
    self.tableView.tableHeaderView = headerV;
    _headerV = headerV;

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view);
    }];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}
- (void)pop{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)initData{
    
    if ([SCUserTool loadInfo].store_type == 2) {
        _data  =  @[@[@{@"icon":@"shop",@"title":@"我的店铺",@"class":[MineShopController class]},
                      @{@"icon":@"zhangdan",@"title":@"我的账单",@"class":[BillViewController class]},
                      @{@"icon":@"me_clear",@"title":@"我的结算单",@"class":[SettlementController class]},
                      @{@"icon":@"person",@"title":@"我的店员",@"class":[MineClerkController class]}],
                    @[ @{@"icon":@"setting",@"title":@"设置",@"class":[SettingController class]}]];
    }else{
        _data  =  @[@[@{@"icon":@"shop",@"title":@"我的店铺",@"class":[MineShopController class]},
                      @{@"icon":@"zhangdan",@"title":@"我的账单",@"class":[BillViewController class]},
                      @{@"icon":@"person",@"title":@"我的店员",@"class":[MineClerkController class]}],
                    @[ @{@"icon":@"setting",@"title":@"设置",@"class":[SettingController class]}]];
    }
    [_tableView reloadData];
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _data.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *sectionA = _data[section];
    return sectionA.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identify"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"identify"];
    }
    NSArray *sectionA = _data[indexPath.section];
    NSDictionary *cellDic = sectionA[indexPath.row];
    cell.textLabel.text = cellDic[@"title"];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.imageView.image = [UIImage imageNamed:cellDic[@"icon"]];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSArray *sectionArr = _data[indexPath.section];
    NSDictionary *dic = sectionArr[indexPath.row];
    if (![dic[@"title"]isEqualToString:@"设置"]) {
        if (![[SCUserTool loadInfo].joinin_state isEqualToString:@"20"]) {
            [SCTool showMessage:@"您还未通过审核"];
            return ;
        }
    }
    if ([dic[@"title"]isEqualToString:@"我的结算单"] || [dic[@"title"]isEqualToString:@"我的店员"]) {
        if ([SCUserTool loadInfo].write_off_member_type == 2) {
            [SCTool showMessage:@"您是店员,无法查看"];
            return;
        }
    }
    
    if ([dic[@"class"] isKindOfClass:[NSString class]]) {
        return;
    }
    UIViewController *vc = [[dic[@"class"] alloc] init] ;
    [self.navigationController pushViewController:vc animated:YES];
    

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableView *)tableView{
    
    if (_tableView == nil) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.rowHeight = 44;
        tableView.separatorColor = WYColor(238, 238, 238);
        tableView.tableFooterView = [[UIView alloc] init];
        [self.view addSubview:tableView];
        _tableView = tableView;
    }
    return _tableView;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}
- (UIRectEdge)edgesForExtendedLayout {
    return UIRectEdgeTop|UIRectEdgeLeft|UIRectEdgeRight;
}
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor clearColor]];

    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

}
- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
}


@end
